<template>
    <div class="environmental-indicators-container">
        <v-chart class="chart" :option="chartOption" v-if="JSON.stringify(chartOption) !== '{}'"/>
    </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, nextTick } from 'vue'
const chartOption = ref({})
const YData = ref<any[]>([])
// 模拟数据
const setInit = () => {
    chartOption.value = {
        tooltip: {
            trigger: 'axis',
            backgroundColor: 'rgba(0, 0, 0, 0.8)',
            borderColor: 'rgba(124, 189, 207, 0.8)',
            textStyle: {
                color: '#ffffff'
            },
            formatter: function (params: any) {
                let result = params[0].name + '<br/>'
                params.forEach((item: any) => {

                    result += `${item.marker}${item.seriesName}: ${item.value}<br/>`
                })
                return result
            }
        },

        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            top: '10%',
            containLabel: true
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24'],
            axisLine: {
                lineStyle: {
                    color: 'rgba(255, 255, 255, 0.3)'
                }
            },
            axisLabel: {
                color: '#ffffff',
                fontSize: 12,
                interval: 1,
            },
        },
        yAxis: [
            {
                type: 'value',
                name: '运行车辆数量',
                position: 'left',
                min: 0,
                max: 25,
                axisLine: {
                    show: false
                },
                axisTick: {
                    show: false
                },
                axisLabel: {
                    color: '#ffffff',
                    fontSize: 12
                },
                splitLine: {
                    lineStyle: {
                        color: 'rgba(255, 255, 255, 0.1)',
                        type: 'dashed'
                    }
                },
                nameTextStyle: {
                    color: '#7CBDCF',
                    fontSize: 12
                }
            }
        ],
        series: [
            {
                name: '运行车辆数量',
                type: 'line',
                data: YData.value,
                smooth: true,
                symbol: 'circle',
                symbolSize: 6,
                yAxisIndex: 0,
                lineStyle: {
                    color: '#00d4ff',
                    width: 3
                },
                itemStyle: {
                    color: '#00d4ff'
                },
                areaStyle: {
                    color: {
                        type: 'linear',
                        x: 0,
                        y: 0,
                        x2: 0,
                        y2: 1,
                        colorStops: [
                            {
                                offset: 0,
                                color: 'rgba(0, 212, 255, 0.3)'
                            },
                            {
                                offset: 1,
                                color: 'rgba(0, 212, 255, 0.05)'
                            }
                        ]
                    }
                },
                showSymbol: false,
            }
        ]
    }
}


// onMounted(() => {
//     // 这里可以添加数据获取逻辑
//     let timer = setTimeout(async() => {
//         await setInit()
//         await clearTimeout(timer)
//     }, 1000)

// })

const timer = ref()
onMounted(async () => {
    await nextTick(() => {
        // 获取chart高度
        timer.value = setTimeout(() => {
            setInit()
            clearTimeout(timer.value)
        }, 1000)
    })
})


const init = (module: any) => {
    let data = JSON.parse(module)
    let list: number[] = []

    if (module) {
        data.forEach((item: any) => {
            list.push(Number(item.value))

        })

        YData.value = list

    }

}

defineExpose({
    init
})
</script>

<style lang="less" scoped>
.environmental-indicators-container {
    width: 100%;
    height: 100%;
    padding: 10px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    .chart {
        flex: 1;
        width: 100%;
        height: 100%;
    }
}

</style>
<!-- <template>
  <div>
    
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';



</script>

<style scoped> 

</style> -->